package vn.anhqn.englishsentensetranslator.adapter;

import java.util.List;

import main.java.com.memetix.mst.language.Language;

import vn.anhqn.englishsentensetranslator.R;
import vn.anhqn.englishsentensetranslator.constant.ConfigApp;
import vn.anhqn.englishsentensetranslator.utils.PreferenceHelper;
import vn.anhqn.englishsentensetranslator.utils.Utils;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.Toast;

public class SelectLanguageAdapter extends BaseAppAdapter<String> implements
		OnClickListener {

	private int indexSelect = 1;

	public SelectLanguageAdapter(List<String> list, Context c) {
		super(list, c);
		indexSelect = PreferenceHelper.getInstance(c).getInt(ConfigApp.PREFERENCES_LANGUAGE_INDEX, 1);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RadioButton radio = (RadioButton) convertView;
		if (radio == null) {
			radio = new RadioButton(getContext());
			Utils.getIntance().setFontFromAsset(radio,
					"Helvetica/helvetica-normal.ttf");
			radio.setTextSize(getContext().getResources().getDimension(
					R.dimen.select_lang_radio_text_size));
			radio.setTextColor(getContext().getResources().getColor(
					R.color.default_text));
		}
		radio.setTag(position);
		radio.setOnClickListener(this);
		String item = getItem(position);
		radio.setText(item);
		radio.setChecked(position == indexSelect);
		return radio;
	}

	public int getIndexSelect() {
		return indexSelect;
	}

	@Override
	public void onClick(View v) {
		indexSelect = ((Integer) v.getTag());
		notifyDataSetChanged();
		//Toast.makeText(getContext(), Language.fromIndex(indexSelect).name(),Toast.LENGTH_SHORT).show();
		// PreferenceHelper.getInstance(getContext()).putInt(ConfigApp.PREFERENCES_LANGUAGE_INDEX,
		// i);
	}
}
